// pages/recruit/recruit.js
const Toptips = require('../../vendor/zanui/toptips/toptips')
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')

const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    stuid: '',
    form: {
      real_name: '',
      sex: '',
      mobile: '',
      other_club: '',
      resume: ''
    },
    isError: {
      real_name: false,
      sex: false,
      mobile: false,
      other_club: false,
      resume: false
    }
  },
  onLoad: function () {
    this.setData({
      'stuid': app.$store.stuid
    })
  },
  handleSexChange: function (event) {
    var value = event.detail.value
    if (this.validator('sex', value)) {
      this.setData({
        "form.sex": value
      })
    }
  },
  handleInputValidate(event) {
    var type = event.target.dataset.name
    var value = event.detail.value
    if (type === 'real_name') {
      value = value.trim()
      this.setData({
        'form.real_name': value
      })
    }
    this.validator(type, value)
    var setObj = {}
    setObj['form.' + type] = value
    this.setData(setObj)
  },
  validator(type, value) {
    var validator = {
      real_name: {
        reg: 'require',
        error: '请填写姓名'
      },
      sex: {
        reg: /^(男|女)$/,
        error: '请选择性别'
      },
      mobile: {
        reg: /^\d{11}$/,
        error: '请填写正确的手机号码'
      },
      other_club: {
        reg: 'require',
        error: '请填写参加其他社团情况'
      },
      resume: {
        reg: 'require',
        error: '请填写参加个人简历'
      }
    }
    var reg = validator[type].reg
    var result
    if (reg === 'require') {
      result = !!value
    } else if (reg === 'function') {
      result = validator[type].function()
    } else {
      result = reg.test(value)
    }
    if (!result) {
      Toptips({
        content: validator[type].error,
        duration: 1000
      })
    }
    var setObj = {}
    setObj['isError.' + type] = !result
    this.setData(setObj)
    return result
  },
  recruit() {
    let stuid = this.data.stuid
    if (['real_name', 'sex', 'mobile', 'other_club', 'resume'].every((type) => this.validator(type, this.data.form[type]))) {
      wx.showLoading({
        title: '注册中',
        mask: true
      });
      qcloud.request({
        url: `${config.service.host}/weapp/user/recruit`,
        method: 'post',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: this.data.form,
        success: res => {
          wx.hideLoading()
          console.log(res)
          if (res.data.code === 0) {
            util.showSuccess(res.data.msg)
            app.$store.recruit_info = this.data.form
            app.$store.recruit_info.stuid = stuid
            wx.redirectTo({
              url: '../recruited/recruited'
            })
          } else if (res.data.code === -2) {
            util.showModel('绑定失败', res.data.error)
            wx.redirectTo({
              url: '../welcome/welcome'
            })
          } else if (res.data.code === -3) {
            util.showModel('绑定失败', res.data.error)
            wx.redirectTo({
              url: '../welcome/welcome'
            })
          } else if (res.data.code === -4) {
            util.showModel('绑定失败', res.data.error)
            wx.redirectTo({
              url: '../welcome/welcome'
            })
          } else if (res.data.code === -5) {
            util.showModel('绑定失败', res.data.error)
            if (this.data.form[res.data.error_param] !== undefined) {
              var setObj = {}
              setObj['isError.' + res.data.error_param] = true
              this.setData(setObj)
            }
          } else {
            console.log(res.data.error);
            util.showModel('绑定失败', res.data.error)
          }
        },
        fail: err => {
          wx.hideLoading()
          console.log(err)
          util.showModel('绑定失败', err)
        }
      })
    }
  }
})